package com.dolphin.business.manager.impl;

import java.util.List;

import com.dolphin.business.common.utils.JSONUtil;
import com.dolphin.business.common.utils.PagerModel;
import com.dolphin.business.entity.User;
import com.dolphin.business.manager.AbstractManager;
import com.dolphin.business.manager.UserManager;
import com.sdicons.json.mapper.JSONMapper;
import com.sdicons.json.mapper.MapperException;

public class UserManagerImpl extends AbstractManager implements UserManager {

	@Override
	public void addUser(User user) {

		getHibernateTemplate().save(user);
	}

	@Override
	public void delUserById(String id) {

		getHibernateTemplate().delete(findUserById(id));
	}

	@Override
	public PagerModel findAllUsers() {

		return searchPaginate("select u from u");
	}

	@Override
	public User findUserById(String id) {

		return (User)getHibernateTemplate().load(User.class, id);
	}

	@Override
	public void updateUser(User user) {

		getHibernateTemplate().update(user);
	}

	@Override
	public String login(String account, String password) {
		String jsonStr = "";
		String hql = "select u from User u where u.account=?";
		User user = (User)getSession().createQuery(hql)
					.setParameter(0, account)
					.uniqueResult();
		if(user != null) {
			if(password.equals(user.getPassword())) {
				jsonStr = "{\"login\":1}";
			} else {
				jsonStr = "{\"login\":2}";
			}
		} else {
			jsonStr = "{\"login\":3}";
		}
//		List list = getSession().createQuery(hql).list();
//		try {
//			jsonStr = JSONMapper.toJSON(list).render(false);
//		} catch (MapperException e) {
//			e.printStackTrace();
//		}
		return jsonStr;
	}

}
